package com.yang.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.yang.bean.Result;
import com.yang.bean.User;
import com.yang.service.IUserService;

/**
 * 用户控制器
 * 
 * @author admin
 *
 */
@Controller
@RequestMapping("/user")
public class UserController {

    /**
     * 自动注入服务层接口
     */
    @Autowired
    private IUserService iuserServcie;

    /**
     * 查询全部操作
     * 
     * @return
     * @return
     * @return
     * @throws Exception
     */
    @RequestMapping("/list")
    @ResponseBody
    public List<User> listUser() throws Exception {
        List<User> list = iuserServcie.searchUser();
        return list;
    }

    /**
     * 模糊查询用户列表
     * 
     * @return
     */
    @RequestMapping("/getcond")
    @ResponseBody
    public List<User> queryLikeUsers(String userName) {
        List<User> uList = iuserServcie.queryLikeUsers(userName);
        return uList;
    }
    /*
     * public Page queryLikeUsers(int pageSize,int pageNum,User user){
     * Map<String,Object> cond=new HashMap<String,Object>(); cond.put("pageSize",
     * pageSize); cond.put("pageNum", pageNum); cond.put("user", user); Page page =
     * iuserServcie.queryLikeUsers(cond); return page; }
     */

    /**
     * 用户登录方法
     * 
     * @param user
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    @ResponseBody
    public String login(User user) {
        User users = iuserServcie.login(user);
        if (null == users) {
            return "error";
        } else {
            return "success";
        }
    }

    /**
     * 查询测试操作
     * 
     * @return
     */
    @RequestMapping(value = "/getInfo", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getInfo() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("name", "张三");
        map.put("passWord", 123453);
        map.put("name", "李四");
        map.put("passWord", 123454);
        return map;
    }

    @RequestMapping("/adduser")
    public ModelAndView addUser(HttpSession session) {
        Map<String, String> csMap = new HashMap<String, String>();
        csMap.put("userName", "李四");
        session.setAttribute("userdept", "sdfs");
        return new ModelAndView("table", csMap);
    }

    /**
     * 增加新用户
     * 
     * @return
     * 
     * @return
     * @throws Exception
     */
    @RequestMapping("/add")
    @ResponseBody
    public int addUser(User user) throws Exception {
        int result = 0;
        if (user != null) {
            user.setCreateTime(new Date());
            user.setUpdateTime(new Date());
            user.setState(1);
            result = iuserServcie.saveUser(user);
        }
        return result;
    }

    /**
     * 删除用户
     * 
     * @return
     * 
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/deluser", method = RequestMethod.POST)
    @ResponseBody
    public int delUser(int id) throws Exception {
        int result = 0;
        if (id > 0) {
            result = iuserServcie.deleteUser(id);
        }
        return result;
    }

    /**
     * 查询单个用户详细信息
     * 
     * @return
     */
    @RequestMapping("/edit")
    public ModelAndView findUser(int id) {
        User rsUser = iuserServcie.getUser(id);
        Map<String, Object> rsMap = new HashMap<String, Object>();
        rsMap.put("data", rsUser);
        return new ModelAndView("user_edit", rsMap);
    }

    /**
     * 编辑用户
     * 
     * @return
     * @throws Exception
     */
    @RequestMapping("/edituser")
    @ResponseBody
    public int editUser(User user) throws Exception {
        int result = 0;
        if (user != null) {
            user.setUpdateTime(new Date());
            result = iuserServcie.updateUser(user);
        }
        return result;
    }

}
